/*
 * jquery.juice.dropdown.js
 *
 * Juice Library Dropdown v0.1.2
 * Date: 2009-04-23
 * Requires: jQuery v1.3 or later
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Copyright 2009 Steve Whiteley (http://jui.ce.it)
 */

(function($) {
	$.fn.dropdown = function(callerSettings) {
		var settings = $.extend({}, $.fn.dropdown.settings, callerSettings);
		return this.each(function() {
			var s = {}, n = $(this);
			var setup = function() {
				s.input = $('<input />')
					.attr({
						name: n.attr('name'),
						type: 'hidden',
						value: n.attr('value')
					})
					.insertAfter(n);
				s.select = $('<span />')
					.addClass('juice-dropdown')
					.insertAfter(n)
					.css('z-index', 1001);
				s.label = $('<a />')
					.addClass('juice-dropdown-label')
					.text($('option:selected', n).text() || $('option:first', n).text())
					.appendTo(s.select);
				s.options = $('<div />')
					.addClass('juice-dropdown-options')
					.css({
						display: 'none',
						position: 'absolute',
						top: 0,
						left: 0,
						zIndex: 1000
					})
					.appendTo('body');
				s.iframe = $('<iframe frameborder="0" />')
					.css({
						width: '100%',
						position: 'absolute',
						top: 0,
						left: 0
					});
				if ($.browser.msie && $.browser.version == 6) {
					s.iframe.appendTo(s.options);
				}
				var groups = $('optgroup', n);
				if (groups.length > 0) {
					groups.each(function(i) {
						var group = $('<div />')
							.addClass('juice-dropdown-group')
							.appendTo(s.options);
						var label;
						if (label = $(this).attr('label')) {
							$('<div />')
								.addClass('juice-dropdown-heading')
								.html(label)
								.appendTo(group);
						}
						$('option', this).each(function() {
							options($(this), group);
						});
						if (i < groups.length - 1) {
							$('<div />')
								.addClass('juice-dropdown-separator')
								.appendTo(s.options);
						}
					});
				} else {
					$('option', n).each(function() {
						options($(this), s.options);
					});
				}
				if ($.isFunction(settings.onSelect)) {
					s.input.bind('onSubmit', settings.onSelect);
				}
				n.remove();
				s.select.click(show);
				s.options.click(function(e) {
					e.stopPropagation();
				});
			};
			var options = function(option, group) {
				$('<a />')
					.html(option.html() || '&nbsp;')
					.addClass(option.attr('class'))
					.attr('href', '#' + option.html())
					.appendTo(group)
					.click(function(e) {
						e.preventDefault();
						s.label
							.hide()
							.html(option.html() || '&nbsp;')
							.fadeIn(settings.fade);
						s.input.val(option.val());
						setTimeout(function() {
							s.input.trigger('onSubmit', [option.val(), option.html()]);
						}, 10);
						close();
					});
			};
			var show = function(e) {
				position();
				s.options
					.css('width', Math.max(settings.minWidth, s.select.innerWidth()))
					.show(settings.fade, function() {
						$(document).click(close);
						s.iframe.css('height', s.options.innerHeight());
					}).focus();
				s.select.addClass('active');
				$(window).bind('resize.dropdown', position);
			};
			var position = function() {
				var pos = s.select.offset();
				s.options
					.css({
						top: pos.top + s.select.outerHeight() + settings.offset,
						left: pos.left,
						width: Math.max(settings.minWidth, s.select.innerWidth())
					});
			};
			var close = function() {
				s.options.hide();
				$(document).unbind('click', close);
				s.select.removeClass('active');
				$(window).unbind('.dropdown');
			};
			setup();
		});
	};
	$.fn.dropdown.settings = {
		offset: 0,
		onSelect: false,
		minWidth: false,
		fade: 1
	};
})(jQuery);